﻿using System;

namespace Quail
{
    /// <summary>
    /// Generic WebPage exception
    /// </summary>
    public class WebPageException : LogStatusException
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebPageException"/> class.
        /// </summary>
        /// <param name="page">The page with the exception</param>
        /// <param name="message">The exception message</param>
        public WebPageException(IWebPage page, string message)
            : this(page, LogStatus.Fail, message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebPageException"/> class.
        /// </summary>
        /// <param name="page">The page with the exception</param>
        /// <param name="status">The status to log</param>
        /// <param name="message">The exception message</param>
        public WebPageException(IWebPage page, LogStatus status, string message)
            : base(status, message)
        {
            Page = page;
            Page.ActiveTask.AddLog(LogStatus.Information, "Exception: " + GetType() + ", Page: " + Page.GetType() + ", URL: " + Page.SafeLocation);
        }

        /// <summary>
        /// Gets the WebPage with the exception
        /// </summary>
        public IWebPage Page { get; private set; }
    }

    /// <summary>
    /// Indicates a page was not found
    /// </summary>
    public class WebPageNotFoundException : WebPageException
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebPageNotFoundException"/> class.
        /// </summary>
        /// <param name="page">The page with the exception</param>
        /// <param name="message">The exception message</param>
        public WebPageNotFoundException(IWebPage page, string message)
            : base(page, LogStatus.Fail, message)
        {
        }
    }

    /// <summary>
    /// A Yellow Screen Of Death (YSOD) was found
    /// </summary>
    public class YSODFoundException : WebPageException
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="YSODFoundException"/> class.
        /// </summary>
        /// <param name="page">The page with the exception</param>
        /// <param name="locationwithysod">The location of the YSOD</param>
        public YSODFoundException(IWebPage page, string locationwithysod)
            : base(page, LogStatus.FailYsod, "YSOD found")
        {
        }
    }

    /// <summary>
    /// A WebPage call timed out
    /// </summary>
    public class WebPageTimedOutException : WebPageException
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebPageTimedOutException"/> class.
        /// </summary>
        /// <param name="page">The page with the exception</param>
        /// <param name="timeout">The timeout (in ms)</param>
        public WebPageTimedOutException(IWebPage page, int timeout)
            : base(page, LogStatus.FailTimeout, "Timed out waiting " + timeout + "ms")
        {
        }
    }
}
